//package com.ruoyi.websql.config;
//
//import cn.hutool.core.net.NetUtil;
//import com.alibaba.druid.filter.logging.Slf4jLogFilter;
//import com.alibaba.druid.filter.stat.StatFilter;
//import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
//import com.alibaba.druid.support.http.StatViewServlet;
//import com.alibaba.druid.support.http.WebStatFilter;
//import com.alibaba.druid.util.Utils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.boot.jdbc.DatabaseDriver;
//import org.springframework.boot.web.servlet.FilterRegistrationBean;
//import org.springframework.boot.web.servlet.ServletRegistrationBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//
//import javax.servlet.*;
//import javax.sql.DataSource;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//
//
///**
// * @ClassName : DruidDataSourceConfiguration
// * @Description : druid配置
// * @Author rabbit boy_0214@sina.com
// * @Date: 2023/8/11 16:42
// */
//@Configuration(value = "dconfig")
//@ConditionalOnClass(com.alibaba.druid.pool.DruidDataSource.class)
//@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.alibaba.druid.pool.DruidDataSource", matchIfMissing = true)
//public class DruidDataSourceConfiguration {
//
//
////    @Value("${druid.login.enabled}")
//    private Boolean enabled=true;
//
////    @Value("${druid.username}")
//    private String username="admin";
//
////    @Value("${druid.password}")
//    private String password="admin123";
//
//    protected <T> T createDataSource(DataSourceProperties properties,
//                                     Class<? extends DataSource> type) {
//        return (T) properties.initializeDataSourceBuilder().type(type).build();
//    }
//
//    @Bean
//    @ConfigurationProperties("spring.datasource.druid")
//    public com.alibaba.druid.pool.DruidDataSource dataSource(DataSourceProperties properties) {
//
//        com.alibaba.druid.pool.DruidDataSource dataSource = createDataSource(
//                properties, com.alibaba.druid.pool.DruidDataSource.class);
//        DatabaseDriver databaseDriver = DatabaseDriver.fromJdbcUrl(properties.determineUrl());
//        String validationQuery = databaseDriver.getValidationQuery();
//        if (validationQuery != null) {
//            dataSource.setTestOnBorrow(true);
//            dataSource.setValidationQuery(validationQuery);
//        }
//        return dataSource;
//    }
//
//    /**
//     * 系统全局拦截，无需单独登录。可自行配置
//     */
//    @Bean
//    public ServletRegistrationBean druidStatViewServlet() {
//        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
//        Map<String, String> initParameters = new HashMap<>(4);
//        initParameters.put("resetEnable", "false");
//        initParameters.put("loginUsername", username);
//        initParameters.put("loginPassword", password);
//        String ip = NetUtil.getLocalhost().getHostAddress() + ",127.0.0.1,localhost";
//        initParameters.put("allow", enabled ? "" : ip);
//        servletRegistrationBean.setInitParameters(initParameters);
//        return servletRegistrationBean;
//    }
//
//    @Bean
//    public FilterRegistrationBean druidStatFilter() {
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//        filterRegistrationBean.setName("druidWebStatFilter");
//        filterRegistrationBean.addUrlPatterns("/*");
//        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.mp4,*.woff2,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
//        return filterRegistrationBean;
//    }
//
//    @Bean
//    @Primary
//    public StatFilter statFilter() {
//        StatFilter statFilter = new StatFilter();
//        statFilter.setSlowSqlMillis(1);
//        statFilter.setLogSlowSql(true);
//        statFilter.setMergeSql(true);
//        return statFilter;
//    }
//
//    @Bean
//    public Slf4jLogFilter logFilter() {
//        return new Slf4jLogFilter();
//    }
//
//    /**
//     * 去除监控页面底部的广告
//     */
//    @SuppressWarnings({"rawtypes", "unchecked"})
//    @Bean
//    public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) {
//        // 获取web监控页面的参数
//        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
//        // 提取common.js的配置路径
//        String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
//        String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
//        final String filePath = "support/http/resources/js/common.js";
//        // 创建filter进行过滤
//        Filter filter = new Filter() {
//            @Override
//            public void init(FilterConfig filterConfig) throws ServletException {
//            }
//
//            @Override
//            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
//                    throws IOException, ServletException {
//                chain.doFilter(request, response);
//                // 重置缓冲区，响应头不会被重置
//                response.resetBuffer();
//                // 获取common.js
//                String text = Utils.readFromResource(filePath);
//                // 正则替换banner, 除去底部的广告信息
//                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
//                text = text.replaceAll("powered.*?shrek.wang</a>", "");
//                response.getWriter().write(text);
//            }
//
//            @Override
//            public void destroy() {
//            }
//        };
//        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//        registrationBean.setFilter(filter);
//        registrationBean.addUrlPatterns(commonJsPattern);
//        return registrationBean;
//    }
//
//
//}
